﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GIMS.IBLL;
using KPIS.Utils;
using Telerik.Web.UI;

namespace KPIS.GERP.GIMS.BLL
{
    public class CommandControl
    {
        public void AddNewCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            //Web.ResetControls(pnList);
            ResetControl(pnList);
            SetPanelList(pnList, "AddNew");
            SetPanelCommand(pnCommand, "AddNew");

            pnList.Visible = true;
            pnGrid.Visible = false;
        }

        public void EditCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            SetPanelList(pnList, "Edit");
            SetPanelCommand(pnCommand, "Edit");

            pnList.Visible = true;
            pnGrid.Visible = false;
        }

        public void CancelCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            foreach (Control ctrl in pnList.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        ((Label)ctrl).Visible = true;
                        break;
                    case "System.Web.UI.WebControls.Literal":
                        ((Literal)ctrl).Visible = true;
                        break;
                    case "System.Web.UI.WebControls.TextBox":
                        ((TextBox)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.DropDownList":
                        ((DropDownList)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.RadioButtonList":
                        ((RadioButtonList)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        ((RadioButton)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        ((CheckBox)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.LinkButton":
                        ((LinkButton)ctrl).Visible = false;
                        break;
                    case "System.Web.UI.WebControls.RequiredFieldValidator":
                        ((RequiredFieldValidator)ctrl).Visible = false;
                        break;

                    // Html Control (*)
                    case "System.Web.UI.HtmlControls.HtmlGenericControl":
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)ctrl).Visible = false;
                        break;
                    
                    // Telerik Control
                    case "Telerik.Web.UI.RadComboBox":
                        ((RadComboBox)ctrl).Visible = false;
                        break;
                    case "Telerik.Web.UI.RadNumericTextBox":
                        ((RadNumericTextBox)ctrl).Visible = false;
                        break;
                    case "Telerik.Web.UI.RadMaskedTextBox":
                        ((RadMaskedTextBox)ctrl).Visible = false;
                        break;

                    // BasicFrame Control
                    case "BasicFrame.WebControls.BasicDatePicker":
                        ((BasicFrame.WebControls.BasicDatePicker)ctrl).Visible = false;
                        break;
                }
            }

            SetPanelCommand(pnCommand, "Cancel");
        }

        public void GridCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            SetPanelCommand(pnCommand, "Grid");

            pnList.Visible = false;
            pnGrid.Visible = true;
        }

        public void ViewCommand(Control pnList, Control pnGrid, Control pnCommand)
        {
            SetViewMode(pnList);

            SetPanelList(pnList, "View");
            SetPanelCommand(pnCommand, "View");

            pnList.Visible = true;
            pnGrid.Visible = false;
        }

        public void SetViewMode(Control pnList)
        {
            // Mapping Label Control to TextBox Control
            foreach (Control ctrl in pnList.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        {
                            Label lb = ((Label)ctrl);

                            foreach (Control mapControl in pnList.Controls)
                            {
                                bool bFound = false;

                                switch (mapControl.GetType().ToString())
                                {
                                    case "System.Web.UI.WebControls.TextBox":
                                        {
                                            if (((TextBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((TextBox)mapControl).Text;
                                                bFound = true;
                                            }

                                            break;
                                        }
                                    case "Telerik.Web.UI.RadComboBox":
                                        {
                                            if (((RadComboBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadComboBox)mapControl).SelectedValue.Length > 0 ? ((RadComboBox)mapControl).SelectedItem.Text : "";
                                                bFound = true;
                                            }

                                            break;
                                        }
                                    case "Telerik.Web.UI.RadNumericTextBox":
                                        {
                                            if (((RadNumericTextBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadNumericTextBox)mapControl).Text.Length > 0 ? String.Format("{0:N2}", ((RadNumericTextBox)mapControl).Value) : "";
                                                bFound = true;
                                            }

                                            break;
                                        }
                                    case "Telerik.Web.UI.RadMaskedTextBox":
                                        {
                                            if (((RadMaskedTextBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadMaskedTextBox)mapControl).Text.Length > 0 ? ((RadMaskedTextBox)mapControl).TextWithLiterals : "";
                                                bFound = true;
                                            }

                                            break;
                                        }
                                    case "System.Web.UI.WebControls.RadioButtonList":
                                        {
                                            if (((RadioButtonList)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadioButtonList)mapControl).SelectedItem.Text.Length > 0 ? ((RadioButtonList)mapControl).SelectedItem.Text : "";
                                                bFound = true;
                                            }

                                            break;
                                        }
                                    case "System.Web.UI.WebControls.RadioButton":
                                        {
                                            if (((RadioButton)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadioButton)mapControl).Text.Length > 0 ? ((RadioButton)mapControl).Text : "";
                                                bFound = true;
                                            }

                                            break;
                                        }
                                }

                                if (bFound)
                                    break;
                            }

                            break;
                        }
                }
            }
        }

        public void SetPanelList(Control pnList, string mode)
        {
            foreach (Control ctrl in pnList.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        ((Label)ctrl).Visible = (mode == "View");
                        break;
                    case "System.Web.UI.WebControls.Literal":
                        ((Literal)ctrl).Visible = (mode == "View");
                        break;
                    case "System.Web.UI.WebControls.TextBox":
                        ((TextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "System.Web.UI.WebControls.DropDownList":
                        ((DropDownList)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "System.Web.UI.WebControls.RadioButtonList":
                        ((RadioButtonList)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        ((RadioButton)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        ((CheckBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "System.Web.UI.WebControls.LinkButton":
                        ((LinkButton)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;

                    // Html Control (*)
                    case "System.Web.UI.HtmlControls.HtmlGenericControl":
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;

                    // Telerix Control
                    case "Telerik.Web.UI.RadComboBox":
                        ((RadComboBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "Telerik.Web.UI.RadNumericTextBox":
                        ((RadNumericTextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit");
                        break;
                    case "Telerik.Web.UI.RadMaskedTextBox":
                        ((RadMaskedTextBox)ctrl).Visible = (mode == "AddNew" || mode == "Edit"); ;
                        break;

                    // BasicFrame Control
                    case "BasicFrame.WebControls.BasicDatePicker":
                        ((BasicFrame.WebControls.BasicDatePicker)ctrl).Visible = (mode == "AddNew" || mode == "Edit"); ;
                        break;
                }
            }
        }

        private void SetPanelCommand(Control pnCommand, string mode)
        {
            foreach (Control ctrl in pnCommand.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.LinkButton":
                    {
                        if (((LinkButton)ctrl).ID.IndexOf("AddNew_") >= 0 || ((LinkButton)ctrl).ID == "AddNew")
                        {
                            ((LinkButton)ctrl).Visible = (mode == "Grid");
                        }
                        else if (((LinkButton)ctrl).ID.IndexOf("Edit_") >= 0 || ((LinkButton)ctrl).ID == "Edit")
                        {
                            ((LinkButton)ctrl).Visible = !(mode == "AddNew" || mode == "Grid" || mode == "Edit") || (mode == "Cancel");
                        }
                        else if (((LinkButton)ctrl).ID.IndexOf("Delete_") >= 0 || ((LinkButton)ctrl).ID == "Delete")
                        {
                            ((LinkButton)ctrl).Visible = (!(mode == "AddNew") && !(mode == "Edit")) || (mode == "Cancel");
                        }
                        else if (((LinkButton)ctrl).ID.IndexOf("AddNewSave_") >= 0 || ((LinkButton)ctrl).ID == "AddNewSave")
                        {
                            ((LinkButton)ctrl).Enabled = (mode == "AddNew");
                            ((LinkButton)ctrl).Visible = (mode == "AddNew");
                        }
                        else if (((LinkButton)ctrl).ID.IndexOf("EditSave_") >= 0 || ((LinkButton)ctrl).ID == "EditSave")
                        {
                            ((LinkButton)ctrl).Enabled = (mode == "Edit");
                            ((LinkButton)ctrl).Visible = (mode == "Edit");
                        }
                        else if (((LinkButton)ctrl).ID.IndexOf("Cancel_") >= 0 || ((LinkButton)ctrl).ID == "Cancel")
                        {
                            ((LinkButton)ctrl).Enabled = (mode == "Edit");
                            ((LinkButton)ctrl).Visible = (mode == "Edit");
                        }
                        else if (((LinkButton)ctrl).ID == "Help")
                        {
                            ((LinkButton)ctrl).Enabled = true;
                            ((LinkButton)ctrl).Visible = true;
                        }
                        else if (((LinkButton)ctrl).ID == "Recycle")
                        {
                            ((LinkButton)ctrl).Visible = (mode == "Grid");
                        }

                        break;
                    }
                }
            }
        }
        public void SetPanel(Panel ajPanel, string Mode)
        {
            bool bView = Mode == "View";
            bool bEdit = Mode == "Edit" || Mode == "AddNew";

            foreach (Control ctrl in ajPanel.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        {
                            Label lb = ((Label)ctrl);

                            foreach (Control mapControl in ajPanel.Controls)
                            {
                                bool bFound = false;

                                switch (mapControl.GetType().ToString())
                                {
                                    case "System.Web.UI.WebControls.TextBox":
                                        {
                                            if (((TextBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((TextBox)mapControl).Text;
                                                bFound = true;
                                            }
                                            ((TextBox)mapControl).Visible = bEdit;
                                            break;
                                        }
                                    case "Telerik.Web.UI.RadComboBox":
                                        {
                                            if (((RadComboBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadComboBox)mapControl).SelectedValue.Length > 0 ? ((RadComboBox)mapControl).SelectedItem.Text : "";

                                                bFound = true;
                                            }
                                            ((RadComboBox)mapControl).Visible = bEdit;
                                            break;
                                        }
                                }

                                if (bFound)
                                    break;
                            }
                            lb.Visible = bView;
                            break;
                        }
                }
            }
        }
        public void SetAjaxPanel(RadAjaxPanel ajPanel, string Mode)
        {
            bool bView = Mode == "View";
            bool bEdit = Mode == "Edit" || Mode == "AddNew";

            foreach (Control ctrl in ajPanel.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        {
                            Label lb = ((Label)ctrl);

                            foreach (Control mapControl in ajPanel.Controls)
                            {
                                bool bFound = false;

                                switch (mapControl.GetType().ToString())
                                {
                                    case "System.Web.UI.WebControls.TextBox":
                                        {
                                            if (((TextBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((TextBox)mapControl).Text;
                                                bFound = true;
                                            }
                                            ((TextBox)mapControl).Visible = bEdit;                                            
                                            break;
                                        }
                                    case "Telerik.Web.UI.RadComboBox":
                                        {
                                            if (((RadComboBox)mapControl).ID == lb.AssociatedControlID)
                                            {
                                                lb.Text = ((RadComboBox)mapControl).SelectedValue.Length > 0 ? ((RadComboBox)mapControl).SelectedItem.Text : "";

                                                bFound = true;
                                            }
                                            ((RadComboBox)mapControl).Visible = bEdit;
                                            break;
                                        }
                                }

                                if (bFound)
                                    break;
                            }
                            lb.Visible = bView;
                            break;
                        }
                }
            }
        }
        public void ResetControl(Control pnList)
        {
            foreach (Control ctrl in pnList.Controls)
            {
                switch (ctrl.GetType().ToString())
                {
                    case "System.Web.UI.WebControls.TextBox":
                        ((TextBox)ctrl).Text = "";
                        break;
                    case "System.Web.UI.WebControls.DropDownList":
                        ((DropDownList)ctrl).SelectedIndex = 0;
                        break;
                    case "System.Web.UI.WebControls.RadioButtonList":
                        ((RadioButtonList)ctrl).SelectedIndex = 0;
                        break;
                    case "System.Web.UI.WebControls.RadioButton":
                        ((RadioButton)ctrl).Checked = false;
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        ((CheckBox)ctrl).Checked = false;
                        break;

                    // Telerix Control
                    case "Telerik.Web.UI.RadComboBox":
                        ((RadComboBox)ctrl).SelectedIndex = -1;
                        ((RadComboBox)ctrl).Text = "";
                        break;
                    case "Telerik.Web.UI.RadNumericTextBox":
                        ((RadNumericTextBox)ctrl).Text = "";
                        break;
                    case "Telerik.Web.UI.RadMaskedTextBox":
                        ((RadMaskedTextBox)ctrl).Text = "";
                        break;
                }
            }
        }
    }
}
